package com.youme.section.shop

import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.LinearLayoutManager
import com.hjq.toast.ToastUtils
import com.hyphenate.easeim.R
import com.salton123.ui.base.YoumeTitleActivity
import com.youme.api.Callback
import com.youme.model.Food
import com.youme.section.shop.adapter.HotCaiAdapter
import com.youme.viewmodel.YouMeApiViewModel
import kotlinx.android.synthetic.main.activity_hotcai.*

class HotcaiActivity : YoumeTitleActivity() {
    val favoriteBeanList = arrayListOf<Food>()
    val adapter: HotCaiAdapter = HotCaiAdapter(favoriteBeanList, this)
    override fun getLayoutId(): Int {
        return R.layout.activity_hotdian
    }

    override fun initViewAndData() {
        initRecycle()
        var mViewModel = ViewModelProvider(this).get(YouMeApiViewModel::class.java)
        mViewModel.newProduct(object : Callback<List<Food>> {
            override fun onSuccess(value: List<Food>) {
                favoriteBeanList.addAll(value)
                adapter.notifyDataSetChanged()
            }

            override fun onFailed(errorCode: Int, errorMessage: String, throwable: Throwable?) {
                println(throwable)
                ToastUtils.show("请求失败,$errorCode,$errorMessage,$throwable")
            }
        })
    }

    override fun getTitleText(): String = "热菜推荐"

    private fun initRecycle() {
        val layoutManager = LinearLayoutManager(this)
        hot_dian_recycleview.layoutManager = layoutManager
        hot_dian_recycleview.adapter = adapter
    }
}